package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.beautyescort.EscortBeatBack;
import com.yanqu.road.entity.beautyescort.EscortReport;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortMgr;
import com.yanqu.road.server.manager.beautyescort.CrossEscortPlayerWrap;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;


@Cmd(code = Protocol.C_BEAUTY_ESCORT_BEAT_BACK_DETAIL_VIEW, desc = "反击详情")
public class CrossBeautyEscortBeatBackDetailCmd extends CrossCommand {


    BeautyEscortProto.BeautyEscortBeatBackDetailReqMsg reqMsg;
    long playerId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = BeautyEscortProto.BeautyEscortBeatBackDetailReqMsg.parseFrom(packet.getBytes());
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        BeautyEscortProto.BeautyEscortBeatBackDetailRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortBeatBackDetailRespMsg.newBuilder();
        CrossEscortPlayerWrap playerWrap = CrossBeautyEscortMgr.getEscortPlayer(reqMsg.getServerId(), reqMsg.getUserId());
        if(playerWrap == null){
            ret = GameErrorCode.E_BEAUTY_ESCORT_USER_NOT_FOUND;
            return;
        }
        EscortReport report = playerWrap.getEscortReportByDartId(reqMsg.getDartId());
        if(report == null || !report.getBeatBack().containsKey(reqMsg.getEnemyUserId())){
            ret = GameErrorCode.E_BEAUTY_ESCORT_SYSTEM_NO_OPEN;
            return;
        }
        EscortBeatBack beatBack = report.getBeatBack().get(reqMsg.getEnemyUserId());
        respMsg.setRet(0);
        respMsg.setReward(beatBack.getRewards());
        respMsg.setCartType(beatBack.getCartType());
        respMsg.setGlamour(beatBack.getGlamour());
        CrossEscortPlayerWrap friendPlayer = CrossBeautyEscortMgr.getEscortPlayer(beatBack.getUserId());
        if(friendPlayer != null){
            respMsg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(friendPlayer.getUserBaseInfo()));
        }
        CrossEscortPlayerWrap enemyPlayer = CrossBeautyEscortMgr.getEscortPlayer(reqMsg.getEnemyUserId());
        if(enemyPlayer != null){
            respMsg.setEnemyUser(PlayerBasePb.parsePlayerBaseTempMsg(enemyPlayer.getUserBaseInfo()));
        }
        MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_BEAUTY_ESCORT_BEAT_BACK_DETAIL_VIEW, respMsg));
    }

    @Override
    public void sendData() {
        if(ret != 0){
            BeautyEscortProto.BeautyEscortBeatBackDetailRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortBeatBackDetailRespMsg.newBuilder();
            respMsg.setRet(ret);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_BEAUTY_ESCORT_BEAT_BACK_DETAIL_VIEW, respMsg));
        }
    }
}
